package avicit.bdp.dds.api.controller;

import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dds.api.service.ProcessResourceConfigService;
import avicit.bdp.dds.dao.entity.ProcessResourceSettings;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 流程资源配置REST接口
 */
@RestController
@Api(tags = "流程资源配置REST接口")
@RequestMapping("/api/bdp/dds/process/resource/conf")
public class ProcessResourceConfigRest {

    private static final Logger logger = LoggerFactory.getLogger(ProcessResourceConfigRest.class);

    @Autowired
    private ProcessResourceConfigService processResourceConfigService;

    /**
     * 查看流程定义资源配置
     */
    @ApiOperation(value = "查看流程资源配置")
    @ApiImplicitParam(name = "processDefinitionId", value = "流程定义id", dataType = "String")
    @GetMapping(value = "/view")
    public ResponseMsg<List<ProcessResourceSettings>> getProcessResourceConfig(@RequestParam String processDefinitionId) {
        if (StringUtils.isBlank(processDefinitionId)) {
            logger.error("参数为空");
            throw new BusinessException("参数为空");
        }
        ResponseMsg<List<ProcessResourceSettings>> responseMsg = new ResponseMsg<>();
        List<ProcessResourceSettings> processResourceConfig = processResourceConfigService.getByProcessDefinitionId(processDefinitionId);
        responseMsg.setResponseBody(processResourceConfig);
        return responseMsg;
    }

    /**
     * 创建流程定义资源配置
     */
    @ApiOperation(value = "创建流程资源配置")
    @PostMapping(value = "/create")
    public ResponseMsg<Boolean> createProcessResourceConfig(@RequestBody List<ProcessResourceSettings> processResourceConfig) {

        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        for (ProcessResourceSettings item : processResourceConfig) {
            item.setId(ComUtil.getId());
            processResourceConfigService.insert(item);
            BdpLogUtil.log4Insert(item);
        }
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    /**
     * 编辑流程定义资源配置
     *
     * @param processResourceConfig 流程定义资源配置
     * @return 创建结果
     */
    @ApiOperation(value = "编辑流程资源配置")
    @PostMapping(value = "/update")
    public ResponseMsg<Boolean> updateProcessResourceConfig(@RequestBody List<ProcessResourceSettings> processResourceConfig) {

        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        for (ProcessResourceSettings item : processResourceConfig) {
            ProcessResourceSettings oldItem = processResourceConfigService.selectByPrimaryKey(item.getId());
            processResourceConfigService.updateByPrimaryKey(item);
            BdpLogUtil.log4Update(item,oldItem);
        }
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    /**
     * 删除流程定义资源配置
     *
     * @param id ID
     * @return 删除结果
     */
    @ApiOperation(value = "通过ID删除流程资源配置")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String")
    @DeleteMapping(value = "/delete")
    public ResponseMsg<Boolean> deleteResourceConfig(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            logger.error("参数为空");
            throw new BusinessException("参数为空");
        }
        ProcessResourceSettings processResourceSettings = processResourceConfigService.selectByPrimaryKey(id);
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        processResourceConfigService.deleteByPrimaryKey(id);
        BdpLogUtil.log4Delete(processResourceSettings);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    /**
     * 删除流程定义资源配置
     *
     * @param processDefinitionId 流程定义ID
     * @return 删除结果
     */
    @ApiOperation(value = "通过流程ID删除流程资源配置")
    @ApiImplicitParam(name = "processDefinitionId", value = "流程定义ID", dataType = "String")
    @DeleteMapping(value = "/deleteByProcessDefinitionId")
    public ResponseMsg<Boolean> deleteByProcessDefinitionId(@RequestParam String processDefinitionId) {
        if (StringUtils.isBlank(processDefinitionId)) {
            logger.error("参数为空");
            throw new BusinessException("参数为空");
        }

        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        processResourceConfigService.deleteByProcessDefinitionId(processDefinitionId);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

}
